package com.dd.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dd.entity.*;

public class DdActivityDAO   extends HibernateDaoSupport {
	
	private static final Log log = LogFactory.getLog(DdActivityDAO.class);
	
	public DdActivity findDdActivityById(int aid) {
		log.debug("find by DdActivity instance");
		try {
			return (DdActivity) getHibernateTemplate().get(
					"com.dd.entity.DdActivity", aid);
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	
	public void saveOrUpdateDA(DdActivity transientInstance) {
		log.debug("saving DdActivity instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (Exception re) {
			re.printStackTrace();
			
		}
	}
	public void saveOrUpdateDAU(DdActivityUser transientInstance) {
		log.debug("saving DdActivityUser instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (Exception re) {
			re.printStackTrace();
			
		}
	}
	
	public DdActivityUser findByAidAndUid(java.lang.Integer aid,java.lang.Integer uid) {
		log.debug("findByAidAndUid ");
		try {
			List list = getHibernateTemplate().find("from DdActivityUser where userid="+uid +" and aid="+aid);
			if(list.size()>0){
				DdActivityUser instance = (DdActivityUser)list.get(0);
				return instance;
			}else{
				return null;
			}
			
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	
	public DdActivityUser findByAidAndPhone(java.lang.Integer aid,java.lang.String phone) {
		log.debug("findByAidAndUid ");
		try {
			List list = getHibernateTemplate().find("from DdActivityUser where phone='"+phone +"' and aid="+aid);
			if(list.size()>0){
				DdActivityUser instance = (DdActivityUser)list.get(0);
				return instance;
			}else{
				return null;
			}
			
		} catch (Exception re) {
			re.printStackTrace();
			return null;
		}
	}
	
	//查找某用户参加过的所有活动
	public List findByUserId(java.lang.Integer uid) {
		log.debug(" findByUserId with id: " + uid);
		try {
			return getHibernateTemplate().find("from DdActivityUser where userid="+uid +" order by id  desc");
			
		} catch (RuntimeException re) {
			re.printStackTrace();
			return null;
		}
	}
	//查找某活动所有参与人
	public List findByAId(java.lang.Integer aid) {
		log.debug(" findByAId with id: " + aid);
		try {
			return getHibernateTemplate().find("from DdActivityUser where aid="+aid +" order by id  desc");
			
		} catch (RuntimeException re) {
			re.printStackTrace();
			return null;
		}
	}
}
